export { parseTime, formatTime } from '@/utils'

function pluralize(time, label) {
  if (time === 1) {
    return time + label
  }
  return time + label + 's'
}

export function timeAgo(time) {
  const between = Date.now() / 1000 - Number(time)
  if (between < 3600) {
    return pluralize(~~(between / 60), ' minute')
  } else if (between < 86400) {
    return pluralize(~~(between / 3600), ' hour')
  } else {
    return pluralize(~~(between / 86400), ' day')
  }
}

/* 数字 格式化*/
export function numberFormatter(num, digits) {
  const si = [
    { value: 1E18, symbol: 'E' },
    { value: 1E15, symbol: 'P' },
    { value: 1E12, symbol: 'T' },
    { value: 1E9, symbol: 'G' },
    { value: 1E6, symbol: 'M' },
    { value: 1E3, symbol: 'k' }
  ]
  for (let i = 0; i < si.length; i++) {
    if (num >= si[i].value) {
      return (num / si[i].value + 0.1).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol
    }
  }
  return num.toString()
}

export function toThousandslsFilter(num) {
  return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
}

export function jobStatusFilter(status) {
  const statusMap = {
    1: '运行',
    2: '停止',
    100: '等待',
    200: '等待'
  }
  return statusMap[status]
}

export function disableFilter(value) {
  return value === 0 ? '启用' : '禁用'
}

export function approverFilter(value) {
  return value === 1 ? '是' : '否'
}

export function activitySyncFilter(value) {
  return value === 0 ? '不同步' : '同步'
}

export function substring25Filter(value) {
  if (value != null) {
    if (value.length > 25) {
      return value.substring(0, 25) + '…'
    } else {
      return value
    }
  } else {
    return null
  }
}

export function description15Filter(value) {
  if (value != null) {
    if (value.length > 15) {
      return value.substring(0, 15) + '…'
    } else {
      return value
    }
  } else {
    return '暂无描述'
  }
}
export function substringName10Filter(value) {
  if (value != null) {
    if (value.length > 10) {
      return value.substring(0, 10) + '…'
    } else {
      return value
    }
  } else {
    return '暂无名称'
  }
}
export function substring10Filter(value) {
  if (value != null) {
    if (value.length > 10) {
      return value.substring(0, 10) + '…'
    } else {
      return value
    }
  } else {
    return null
  }
}
export function substring15Filter(value) {
  if (value != null) {
    if (value.length > 15) {
      return value.substring(0, 15) + '…'
    } else {
      return value
    }
  } else {
    return null
  }
}
export function substring8Filter(value) {
  if (value != null) {
    if (value.length > 8) {
      return value.substring(0, 8) + '…'
    } else {
      return value
    }
  } else {
    return null
  }
}
export function description5Filter(value) {
  if (value != null) {
    if (value.length > 5) {
      return value.substring(0, 5) + '…'
    } else {
      return value
    }
  } else {
    return '暂无'
  }
}

export function numFormat(value) {
  if (!value) return ' '
  return Number(value).toFixed(0).toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') // 将整数部分逢三一断
}

export function examineFilter(value) {
  if (value === 2) {
    return '提交审核'
  } else if (value === 3) {
    return '通过审核'
  } else if (value === 4) {
    return '拒绝审核'
  } else {
    return ''
  }
}

export function deployFilter(value) {
  if (value === 1) {
    return '已发布'
  } else if (value === 2) {
    return '未发布'
  } else {
    return ''
  }
}
export function dateFormatFilter(value) {
  // eslint-disable-next-line no-extend-native
  Date.prototype.Format = function(fmt) { // author: meizz
    var o = {
      'Y+': this.getFullYear(), // 年
      'M+': this.getMonth() + 1, // 月
      'd+': this.getDate(), // 日
      'h+': this.getHours(), // 小时
      'm+': this.getMinutes(), // 分
      's+': this.getSeconds(), // 秒
      'q+': Math.floor((this.getMonth() + 3) / 3), // 季度
      'S': this.getMilliseconds() // 毫秒
    }
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length))
    for (var k in o) { if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length))) }
    return fmt
  }
  if (value != null) {
    return new Date(value).Format('yyyy/MM/dd')
  } else {
    return ''
  }
}
